﻿using CodeGenerator.ClassServices;
using CodeGenerator.Models;
using CodeGenerator.Template;
using CodeGenerator.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CodeGenerator.Generators
{
    internal class MVC3WebGenerator : Generator
    {
        public override void Generate(int projectId, string outputPath)
        {
            //CodeGenBLClient client = new CodeGenBLClient();
            //ActionResultDtoClassModelDto result = client.GetDestRelationshipClass(new AttributePkDto { Id = 428 });

            //CodeGenBLClient client = new CodeGenBLClient();
            //ActionResultDtoClassModelDto result = client.GetSourceRelationshipClassByClassId(new ClassPkDto { ClassId = 2 });

            MetaData modelForWeb = new MetaData();
            modelForWeb.Models = this.GetClassesByProjectId(projectId);
            modelForWeb.Project = this.GetProjectByProjectId(projectId);

            List<LookupClassModel> list = this.GetLookupClass(modelForWeb.Models);

            Generate(modelForWeb, list, outputPath);
        }

        public void Generate(MetaData modelForWeb, List<LookupClassModel> list, string outputPath)
        {
            //CodeGenBLClient client = new CodeGenBLClient();
            //ActionResultDtoArrayOfLookupModelDto result = client.GetLookupModelByDestinationClass(new ClassPkDto { ClassId = 2 });
            
            if (!(modelForWeb.Models == null || modelForWeb.Project == null || list == null))
            {
                outputPath = Path.Combine(outputPath, modelForWeb.Project.ProjectName);
                DirectoryExt.CreateIfMissing(outputPath);

                GenerateFiles(modelForWeb, list, "Web", outputPath);
                GenerateFiles(modelForWeb, list, "Web.Controllers", outputPath);
                GenerateFiles(modelForWeb, list, "Web.Content", outputPath);
                GenerateFiles(modelForWeb, list, "Web.Scripts", outputPath);
                GenerateFiles(modelForWeb, list, "Web.CustomHelpers", outputPath);
                GenerateFiles(modelForWeb, list, "Web.ListCenter", outputPath);
                GenerateFiles(modelForWeb, list, "Web.Mapper", outputPath);
                GenerateFiles(modelForWeb, list, "Web.Models", outputPath);
                GenerateFiles(modelForWeb, list, "Web.Properties", outputPath);
                GenerateFiles(modelForWeb, list, "Web.Utilities", outputPath);
                GenerateFiles(modelForWeb, list, "Web.Views", outputPath);
                GenerateFiles(modelForWeb, list, "Web.Views.Shared", outputPath);
                GenerateFiles(modelForWeb, list, "Web.Views.Admin", outputPath);
                //Edit by Soravit 06/04/2014
                //ไปใช้ admin menu แทน
                //GenerateFiles(modelForWeb, list, "Web.Views.InvokeDB", outputPath);
            }
        }
    }
}
